<template>
    <div class="video">
        <p class="header_detail">视频</p>
        <!-- 添加图片 -->
        <div class="detail">
            <p class="form_title">视频封面图</p>
        </div>
        <div class="uploadImg">
            <div class="delete" @click='clearImg' v-if='data.imageUrl'></div>
            <el-upload
                class="avatar-uploader"
                action='#'
                :auto-upload="false"
                :show-file-list="false"
                :on-change='getImg'
                :on-remove="handleRemove"
                :http-request="uploadImg"
                >
                <img v-if="data.imageUrl" :src="data.imageUrl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                <div class="img_loading">
                    <p v-if='!data.imageUrl'>添加图片</p> 
                </div>
            </el-upload>
        </div>
        <div class="detail">
           <div  class="eventBus">
                <p class="event_title" >添加方式</p>
                <el-radio-group v-model="data.locationMethod" @change='toParent'>
                    <el-radio label="1">本地上传</el-radio>
                    <el-radio label="2">链接地址</el-radio>
                </el-radio-group> 
            </div> 
        </div>
        <div class="progressBox" v-if='data.locationMethod=="1"'>
            <el-progress :percentage="precent" v-if='precent!=100 && !data.videoUrl'></el-progress>
            <el-progress :percentage="100" v-if='precent==100 && data.videoUrl' status="success"></el-progress>
        </div>
        <div class="uploadImg" v-if='data.locationMethod==1'>
            <div class="delete" @click='clearFile' v-if='data.videoUrl'></div>
            <el-upload
                class="avatar-uploader"
                action='#'
                :auto-upload="false"
                :show-file-list="false"
                :on-change='getFileRes'
                :on-remove="handleRemove"
                :http-request="uploadImg"
                >
                <video  v-if="data.videoUrl" :src="data.videoUrl" autoplay="autoplay" controls="controls" :loop='data.loop' >
                    your browser does not support the video tag
                </video>
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                <div class="img_loading">
                    <p v-if='!data.imageUrl && !data.videoUrl'>添加视频</p> 
                </div>
            </el-upload>
        </div>
        <div >
            <el-form :model='data'>
                <el-form-item label='链接地址'>
                    <el-input v-model='data.videoUrl' :disabled='data.locationMethod==1'  @change="toParent"></el-input>
                </el-form-item>
                <el-form-item label='自动循环播放'>
                     <el-radio-group v-model='data.loop' @change='toParent'>
                        <el-radio :label="true">是</el-radio>
                        <el-radio :label="false">否</el-radio>
                    </el-radio-group> 
                </el-form-item>
            </el-form>
        </div>
        <div class="detail detail_set">
            <!-- 上下边距-->
            <div class="block">
                <p class="form_title">上边框距离  <span>(0为贴合，最大100像素)</span></p>
                <el-slider
                    v-model="data.paddingTop"
                    show-input
                    :max='100'
                    :step="1"
                    @change='toParent'>
                </el-slider>
            </div>
            <div class="block">
                <p class="form_title">下边框距离  <span>(0为贴合，最大100像素)</span></p>
                <el-slider
                    v-model="data.paddingBottom"
                    show-input
                    :max='100'
                    :step="1"
                    @change='toParent'>
                </el-slider>
            </div>
        </div>
        <div  class="eventBus">
            <p class="event_title" >事件（选填）</p>
            <el-input @change='toParent' v-model='data.event' :disabled="data.eventChecked" placeholder="请输入事件ID"></el-input>
        </div>
        
    </div>
</template>

<script>
import {  uploadImg} from 'api/upload'
export default {
    data(){
        return {
            data:{
                
            },
            imgHeight:200,
            precent:0,
        }
    },
    props:{
        propData:{
            type:Object
        },
        totalList:{
            type:Array
        },
    },
    methods:{
        clearImg(){
            this.data.imageUrl = '';
            this.toParent();
        },
        clearFile(){
            this.data.videoUrl ='';
            this.toParent();
        },
        uploadImg(){

        },
        handleRemove(){

        },
        getIndex(val){
        },
        getImg(val){
            let that = this;
            let infoId = this.$route.query.id;
            var file = val.raw;
            var formdata=new FormData();// 创建form对象
            formdata.append('file',file,file.name);
            uploadImg(formdata).then(res=>{
                if(res){
                    var image = new Image()
                    image.src = res;
                    that.data.imageUrl= res;
                    image.onload =function(){
                        var naturalWidth = image.width;
                        var  naturalHeight = image.height*375/naturalWidth;
                        if(that.imgHeight < naturalHeight){
                            that.data.imgHeight = naturalHeight;
                            that.imgHeight = naturalHeight;
                        }
                        that.toParent();
                    }
                }
            })
            // var reader = new FileReader();
            // this.$ajax({
            //     method:'post',
            //     url:'/base/systemFile/insertCmsPictureAndMp4',
            //     data:file
            // })
            // reader.readAsDataURL(file);
            // reader.onload =(e)=>{ 
            //     that.data.imageUrl=e.target.result;
            //     var image = new Image()
            //     image.src = e.target.result
            //     image.onload =function(){
            //         var naturalWidth = image.width;
            //         var  naturalHeight = image.height*375/naturalWidth;
            //         if(that.imgHeight < naturalHeight){
            //             that.data.imgHeight = naturalHeight;
            //             that.imgHeight = naturalHeight;
            //         }
            //         that.toParent();
            //     }
            // }
        },
        // 上传视频回调
        getFileRes(val){
            let  that= this;
            that.data.videoUrl = '';
            this.toParent();
            this.getFile(val,(res)=>{
                that.precent = Math.round(res.loaded*100/res.total);
            },(suc)=>{
               that.data.videoUrl = suc.result;
               that.toParent();
            })
        },
        getFile(val,callback1,callback2){
            let that = this;
            let infoId = this.$route.query.id;
            var file = val.raw;
            var videoType=["avi","wmv","mkv","mp4","mov","rm","3gp","flv","mpg","rmvb"];
            if(!RegExp("\.(" + videoType.join("|") + ")$", "i").test(file.name.toLowerCase())) {
               return this.$message.error('请选择视频文件上传');
            }
            if(file.size/1048576 > 50){
                return this.$message.error('请选择小于10M的视频文件')
            }
            // var policy=btoa(JSON.stringify({"bucket": bucketname, "save-key": save_key, "expiration": parseInt(Date.parse(new Date())+3600)}));
            var formData=new FormData();
            formData.append('file',file,file.name); 
            // formData.append("policy",policy);
            // formData.append("authorization",signature);
            uploadImg(formData).then(res=>{
                if(res){
                    debugger;
                    that.data.videoUrl = res
                    that.toParent();

                }
            })
            // that.$ajax({
            //     method:'post',
            //     url:'/base/thirdFile/uploadFile',
            //     data:formData,
            //     onUploadProgress:function(progressEvent){ //原生获取上传进度的事件
            //         if(progressEvent.lengthComputable){
            //             //属性lengthComputable主要表明总共需要完成的工作量和已经完成的工作是否可以被测量
            //             //如果lengthComputable为false，就获取不到progressEvent.total和progressEvent.loaded
            //             callback1(progressEvent);
            //         }
            //     }
            // }).then(res =>{
            //     callback2(res.data);
            // })
        },
        beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg';
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG) {
            this.$message.error('上传头像图片只能是 JPG 格式!');
            }
            if (!isLt2M) {
            this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        },
        toParent(){
            let data = {
                ...this.data
            }
            this.$emit('toParent',data)
        }
    },
    watch:{
        propData:function(val){
           this.data = JSON.parse(JSON.stringify(this.propData)); 
        }
    },
    mounted(){
        this.data = JSON.parse(JSON.stringify(this.propData)); 
        this.toParent();
        console.log(this.propData,'接收到的参数')
        // debugger;
    }    

}
</script>
<style lang="less" scoped>
    .video{
        margin-bottom:80px;
    }
// 轮播图
    .header_detail{
        height:50px;
        line-height:50px;
        // border-bottom:1px solid #E4E4E4;
        font-size: 16px;
        color: #000000;
        padding-left:16px;
    }
    .detail{
        padding: 16px 16px 0 16px;
        border-top: 1px solid #E4E4E4;
        font-size: 14px;
        .form_title{
            position:relative;
            left:-10px;
            margin-top: 10px;
            span{
                color: #888888;
            }
        }
        .detail_explain{
            font-size: 12px;
            line-height: 17px;
            color: #888888;
        }
        .detail_content{
            width:100%;
            ul{
                width:100%;
                height:100%;
            }
            li{
                width:100%;
                margin-top: 20px;
            }
            .imgBox{
                width:100%;
                height:100%;
                display:flex;
                justify-content: space-between;
                align-items: center;
                .left{
                    width:150px;
                    height:100px;
                    display:flex;
                    justify-content: center;
                    align-items: center;
                    .left_btns{
                        width:50px;
                        height:100%;
                    }
                    .left_imgs{
                        width:100px;
                        height:100%;
                        background:gray;
                    }
                }
                .center{
                    width:250px;
                    height:100px;
                }
                .right{
                    width:60px;
                    height:auto;
                    &:hover{
                        color:red;
                    }
                }
            }
            .addBtns{
                text-align:center;
                line-height:30px;
                span{
                    color:blue;
                    
                }
            }
        }
    }
    .detail_set{
        padding: 16px 16px 0 25px;
    }
    .uploadImg{
        width:100%;
        height:auto;
        position:relative;
        padding:24px 16px 17px 16px;
        margin-top:16px;
        box-sizing: border-box;
        background:#F7F7F7;
        line-height:35px;
        font-size:14px;
        color:#666666;
        video{
            width:100%;
            height:140px;
        }
        .delete{
            width:14px;
            height:14px;
            position:absolute;
            top: 8px;
            right:6px;
            z-index: 10;
            background:url('~@/assets/img/editContent/icon-ashbin-nor.png') center center/14px 14px  no-repeat;
        }
        .delete:hover{
            background:url('~@/assets/img/editContent/icon-ashbin-hov.png') center center/14px 14px  no-repeat;                    
        }
         /deep/ .avatar-uploader .el-upload {
            border: 1px dashed #FF8244;
            box-sizing: border-box;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
            width:100%;
            height:140px;         
            display:flex;
            justify-content: center;
            align-items:center;
            flex-direction: column;
            color:#FF8244;
        }
        /deep/ .avatar-uploader .el-upload:hover {
            // border-color: #FF8244;
        }
        /deep/.avatar-uploader-icon {
            font-size: 22px;  
            color:#FF8244;
        }
        /deep/.avatar {
            width:100%;
            height:140px;
            display: block;
        }
    }
    /deep/.el-slider__bar{
        background-color:#FF8244;
    }
    /deep/.el-slider__button{
        border: 2px solid #FF8244;
    }
    .eventBus{
        font-size: 14px;
        padding:5px 16px 0 16px;
        // margin-bottom:40px;
        .event_title{
           margin-bottom:10px;
        }
    }
    .progressBox{
        position: relative;
        margin-left: 20px;
        top: 10px;
    }
</style>